package controller;

import dao.ControladorDAOException;
import dao.UsuarioDAO;
import dao.VeiculoDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UsuarioBean;
import model.VeiculoBean;
import dao.impl.*;
import javax.servlet.RequestDispatcher;

public class Controlador extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String cmd = request.getParameter("cmd");

        if (cmd == null) {
            cmd = "default";
        }

        UsuarioBean usuario = new UsuarioBean();
        VeiculoBean veiculo = new VeiculoBean();

        if (cmd.equalsIgnoreCase("addUser") || cmd.equalsIgnoreCase("delUser")
                || cmd.equalsIgnoreCase("editUser")
                || cmd.equalsIgnoreCase("busUser")) {
            usuario.setNome(request.getParameter("nome"));
            usuario.setTelefone(request.getParameter("telefone"));
            usuario.setCpf(request.getParameter("cpf"));
            usuario.setEmail(request.getParameter("email"));
            usuario.setDataNascimento(request.getParameter("dataNascimento"));
            usuario.setUsuario(request.getParameter("usuario"));
            usuario.setSenha(request.getParameter("senha"));
        } else if (cmd.equalsIgnoreCase("addVeiculo") || cmd.equalsIgnoreCase("delVeiculo")
                || cmd.equalsIgnoreCase("editVeiculo") || cmd.equalsIgnoreCase("busVeiculo")) {
            veiculo.setModelo(request.getParameter("modelo"));
            veiculo.setCilindradas(request.getParameter("cilindradas"));
            veiculo.setPrecoDiaria(request.getParameter("precoDiaria"));
            veiculo.setPlaca(request.getParameter("placa"));
        }

        UsuarioDAO usuarioDAO;
        VeiculoDAO veiculoDAO;

        try {
            usuarioDAO = new UsuarioDAOImpl();
            veiculoDAO = new VeiculoDAOImpl();
            RequestDispatcher rd = null;

            if (cmd.equalsIgnoreCase("addUser")) {
                usuarioDAO.salvar(usuario);
                rd = request.getRequestDispatcher("view/mostraUsuario.jsp");
            } else if (cmd.equalsIgnoreCase("delUser")) {
                usuarioDAO.excluir(usuario);
                rd = request.getRequestDispatcher("view/mostraUsuario.jsp");
            } else if (cmd.equalsIgnoreCase("editUser")) {
                usuarioDAO.editar(usuario);
                rd = request.getRequestDispatcher("view/mostraUsuario.jsp");
            } else if (cmd.equalsIgnoreCase("busUser")) {
                usuarioDAO.procurarUsuario(usuario.getCpf());
                rd = request.getRequestDispatcher("view/mostraUsuario.jsp");
            } else if (cmd.equalsIgnoreCase("addVeiculo")) {
                veiculoDAO.salvarVeiculo(veiculo);
                rd = request.getRequestDispatcher("view/mostraVeiculo.jsp");
            } else if (cmd.equalsIgnoreCase("delVeiculo")) {
                veiculoDAO.excluirVeiculo(veiculo);
                rd = request.getRequestDispatcher("view/mostraVeiculo.jsp");
            } else if (cmd.equalsIgnoreCase("editVeiculo")) {
                veiculoDAO.atualizarVeiculo(veiculo);
                rd = request.getRequestDispatcher("view/mostraVeiculo.jsp");
            } else if (cmd.equalsIgnoreCase("busUser")) {
                veiculoDAO.procurarVeiculo( veiculo.getModelo());
                rd = request.getRequestDispatcher("view/mostraVeiculo.jsp");
            }

            rd.forward(request, response);
        } catch (ControladorDAOException e) {
        } finally {
            out.close();


        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
